﻿using System;
using Castle.Windsor;

namespace easyccms.WebUI.Infrastructure
{
  public static class DependencyInjector
  {
    /// <summary>
    /// Private instance of castle windsor container.
    /// </summary>
    private static IWindsorContainer _windsorContainer;

    /// <summary>
    /// Gets the windsor container and if does not exists create new instace.
    
    /// </summary>
    private static IWindsorContainer WindsorContainer
    {
      get { return _windsorContainer ?? (_windsorContainer = CreateWindsorContainer()); }
    }

    /// <summary>
    /// Creates the windsor container.
    /// </summary>
    /// <returns></returns>
    private static IWindsorContainer CreateWindsorContainer()
    {
      IWindsorContainer container = WindsorConfigure.GetConfiguration();

      return container;
    }
    /// <summary>
    /// Resolves this instance.
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <returns>/// Is callded as:  DependencyInjector.Resolve<IPavel>();</returns>
    public static T Resolve<T>()
    {
      return WindsorContainer.Resolve<T>();
    }

    public static object Resolve(Type type)
    {
      return WindsorContainer.Resolve(type);
    }

    public static T Resolve<T>(string name)
    {
      return WindsorContainer.Resolve<T>(name);
    }


    public static void Release(object myObject)
    {
      WindsorContainer.Release(myObject);
    }
  }
}
